﻿using MediatR;
using T.S.BaseData.Api.AppClinet.Commend.Cultivationinfo;
using T.S.BaseData.Domain.Entity;
using T.S.BaseData.ErrorCode;
using T.S.BaseData.Infrastructure.Base;

namespace T.S.BaseData.Api.AppClinet.CommendHand.Cultivationinfo
{
    public class HealthDelCommandHandle : IRequestHandler<HealthDelCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<Health> healRepo;
        private readonly ILogger<HealthDelCommandHandle> logger;

        public HealthDelCommandHandle(IBaseRepository<Health> healRepo, 
            ILogger<HealthDelCommandHandle> logger)
        {
            this.healRepo = healRepo;
            this.logger = logger;
        }

        public Task<ApiResult<int>> Handle(HealthDelCommand request, CancellationToken cancellationToken)
        {

            ApiResult<int> result = new ApiResult<int>();

            var model = healRepo.GetModel(request.Id);
            model.IsDelete = true;

            var r = healRepo.Update(model);
            if (r>0)
            {
                result.Code = ApiEnums.Success;
                result.Data = r;
                result.Meg = "删除成功";

                logger.LogInformation("删除成功");

            }
            else
            {
                result.Code = ApiEnums.Error;
                result.Data = r;
                result.Meg = "删除失败";

                logger.LogInformation("删除失败");
            }

            return Task.FromResult(result);
        }
    }
}
